package com.ruoyi.common.utils.js;


import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ItemCodeOverData {
    public static Integer GetSingleType(Double monitorValue, List<Standard> standards, @RequestParam(value = "itemCode", required = false, defaultValue = "") String itemCode, String sourceValue, @RequestParam(value = "itemCategroy", required = false, defaultValue = "0")Integer itemCategroy)
    {
        try
        {
            if (standards == null && !itemCode.toUpperCase().equals("15DE941F-A221-4D9C-8387-48B0E99F6E25")) {
                return null;
            }
            if (standards != null && standards.size() < 5 && !itemCode.toUpperCase().equals("15DE941F-A221-4D9C-8387-48B0E99F6E25")) {
                return null;
            }

            int singleType = 0;
            if (itemCategroy == 1 && itemCode.toUpperCase().equals("15DE941F-A221-4D9C-8387-48B0E99F6E25") && monitorValue > 0) {
                if (monitorValue >= 6.5 && monitorValue <= 8.5)
                    singleType = 1;
                else if ((monitorValue >= 5.5 && monitorValue < 6.5) || (monitorValue > 8.5 && monitorValue <= 9))
                    singleType = 4;
                else if (monitorValue < 5.5 || monitorValue > 9)
                    singleType = 5;
                return singleType;
            }//pH的地下水
            if (itemCategroy == 0 &&itemCode.toUpperCase().equals("15DE941F-A221-4D9C-8387-48B0E99F6E25") && monitorValue > 0)
            {
                if (monitorValue >= 6.0 && monitorValue <= 9.0)
                    singleType = 1;
                else
                    singleType = 6;
                return singleType;
            }//pH的地表水水质类别计算
            List<Standard> newStandards = new ArrayList<>();
            for (Standard s:standards){
                String level = s.getLevel();
                if(level.equals("1") || level.equals("2")||level.equals("3") || level.equals("4") || level.equals("5")){
                    newStandards.add(s);
                }
            }
            if(null != newStandards && !newStandards.isEmpty()) {
                Standard firstStand =  newStandards.stream().sorted(Comparator.comparing(Standard::getLevel)).collect(Collectors.toList()).get(0);
                String compareMothed = firstStand.getCompareMethod();// 0<= 1>=，2>，3有或无，4未检出
                String standardCategory = firstStand.getStandardCategory();
                if (standardCategory.equals("地下水质量标准"))
                {
                    if ((monitorValue == null || monitorValue <= 0) && !compareMothed.equals("3") && !compareMothed.equals("4"))
                    {
                        return null;
                    }
                    if ((sourceValue==null || sourceValue.trim().equals(""))  && (compareMothed.equals("3") || compareMothed.equals("4")))
                    {
                        return null;
                    }
                    List<Standard> StandList = newStandards.stream().sorted(Comparator.comparing(Standard::getLevel)).collect(Collectors.toList());// 对Level正序排序
                    if (compareMothed.equals("0"))
                    {
                        List<Standard> levelList = new ArrayList<>();
                        for(Standard s : StandList){
                            if(monitorValue<=Double.valueOf(s.getStandardValue())){
                                levelList.add(s);
                            }
                        }
                        Standard sta = null;
                        if(levelList!=null&&levelList.size()>0){
                            sta = levelList.stream().min(Comparator.comparing(Standard::getLevel)).get();
                        }
                        Integer level =sta==null? 5: Integer.valueOf(sta.getLevel());
                        singleType = level==null?5: level;
                    }
                    else if (compareMothed == "1")
                    {
                        List<Standard> levelList = new ArrayList<>();
                        for(Standard s : StandList){
                            if(monitorValue>=Double.valueOf(s.getStandardValue())){
                                levelList.add(s);
                            }
                        }
                        Standard sta = null;
                        if(levelList!=null&&levelList.size()>0){
                            sta = levelList.stream().min(Comparator.comparing(Standard::getLevel)).get();

                        }
                        Integer level =sta==null? 6: Integer.valueOf(sta.getLevel());
                        singleType = level==null?6: level;
                    }
                    else if (compareMothed == "2")
                    {
                        List<Standard> levelList = new ArrayList<>();
                        for(Standard s : StandList){
                            if(monitorValue>Double.valueOf(s.getStandardValue())){
                                levelList.add(s);
                            }
                        }
                        Standard sta = null;
                        if(levelList!=null&&levelList.size()>0){
                            sta = levelList.stream().min(Comparator.comparing(Standard::getLevel)).get();
                        }
                        Integer level =sta==null? 6: Integer.valueOf(sta.getLevel());
                        singleType = level==null?6: level;
                    }
                    else if (compareMothed == "3")
                    {
                    /*var level = standards.OrderBy(a => a.Level).FirstOrDefault(a => SourceValue == a.StandardValue)?.Level;
                    Standard stan = StandList.stream().filter(StandardValue->StandardValue.getCompareMethod())
                    singleType = int.TryParse(level, out outInt) ? outInt : 6;*/
                    }
                    else if (compareMothed == "4")
                    {
                    /*var level = (!string.IsNullOrEmpty(SourceValue) && (SourceValue.Contains("未检出") || SourceValue.ToUpper().Contains("L"))) ? "1" : standards.Where(a => a.Level != "1").OrderBy(a => a.Level).FirstOrDefault(a => monitorValue <= Convert.ToDecimal(a.StandardValue))?.Level;
                    singleType = int.TryParse(level, out outInt) ? outInt : 5;*/
                    }
                    else
                    {
                        return null;
                    }
                }
                else {
                    List<Standard> StandList = newStandards.stream().sorted(Comparator.comparing(Standard::getLevel).reversed()).collect(Collectors.toList());// 对Level倒序排序
                    if (compareMothed.equals("0")) {

                        //比对方法：比对符号为<=，即标准评级为监测值越小越好
                        Integer latestType = 6;//上一轮比对水质级别标准编码,初始默认最差水质
                        Double latestStdValue = 0.0;//上一轮比对水质级别标准值

                        for (Standard st : StandList) {
                            if (monitorValue > Double.parseDouble(st.StandardValue) && (monitorValue <= latestStdValue || latestStdValue == 0.0)) {
                                singleType = latestType;
                            } else if (monitorValue <= Double.parseDouble(st.StandardValue)) {
                                singleType = Integer.parseInt(st.Level);
                            }
                            latestType = Integer.parseInt(st.Level);
                            latestStdValue = Double.parseDouble(st.StandardValue);
                        }
                    } else if (compareMothed.equals("1")) {
                        //比对方法：比对符号为>=，即标准评级为监测值越大越好
                        Integer latestType = 6;//上一轮比对水质级别标准编码,初始默认最差水质
                        Double latestStdValue = 0.0;//上一轮比对水质级别标准值
                        for (Standard st : StandList) {
                            if (monitorValue < Double.parseDouble(st.StandardValue) && (monitorValue >= latestStdValue || latestStdValue == 0.0)) {
                                singleType = latestType;
                            } else if (monitorValue >= Double.parseDouble(st.StandardValue)) {
                                singleType = Integer.parseInt(st.Level);
                            }
                            latestType = Integer.parseInt(st.Level);
                            latestStdValue = Double.parseDouble(st.StandardValue);
                        }
                    } else if (compareMothed.equals("2")) {
                        //比对方法：直接填写文字，有或者无
                    }
                }
            }

            return singleType;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
